from bs4 import BeautifulSoup
# 方法选择器
# find_all(name,attrs,recursive,text,**kwargs)
html = '''
<div class="panel">
<div class="panel-heading">
<h4>Hello</h4>
</div>
<div class="panel-body">
<ul class="list" id="list-1">
<li class="element">Foo</li>
<li class="element">Bar</li>
<li class="element">Jay</li>
</ul>
<ul class="list list-samll" id="list-2">
<li class="element">Foo</li>
<li class="element">Bar</li>
</ul>
</div>
</div>
'''
soup = BeautifulSoup(html,'lxml')
print(type(soup.find_all(name='ul')))
print(soup.find_all(name='ul'))
print(type(soup.find_all(name='ul')[0]))
for ul in soup.find_all(name='ul'):
    print(ul.find_all(name='li'))
    for li in ul.find_all(name='li'):
        print(li.string)
html = '''
<div class="panel">
<div class="panel-heading">
<h4>Hello</h4>
</div>
<div class="panel-body">
<ul class="list" id="list-1" name="elements">
<li class="element">Foo</li>
<li class="element">Bar</li>
<li class="element">Jay</li>
</ul>
<ul class="list list-samll" id="list-2">
<li class="element">Foo</li>
<li class="element">Bar</li>
</ul>
</div>
</div>
'''
# attrs
soup = BeautifulSoup(html,'lxml')
print(soup.find_all(attrs={'id':'list-1'}))#字典类型,返回结果是列表类型
print(soup.find_all(attrs={'name':'elements'}))
print(soup.find_all(id = 'list-1'))
print(soup.find_all(class_ = 'element'))
# text  用来匹配节点的文本，传入的形式可以是字符串，可以是正则表达式对象
import re
html = '''
<div class="panel">
<div class="panel-body">
<a>Hello,this is a link</a>
<a>Hello,this is a link,too</a>
</div>
</div>
'''
soup = BeautifulSoup(html,'lxml')
print(soup.find_all(text=re.compile('link')))


# find()返回单个元素
html = '''
<div class="panel">
<div class="panel-heading">
<h4>Hello</h4>
</div>
<div class="panel-body">
<ul class="list" id="list-1" name="elements">
<li class="element">Foo</li>
<li class="element">Bar</li>
<li class="element">Jay</li>
</ul>
<ul class="list list-samll" id="list-2">
<li class="element">Foo</li>
<li class="element">Bar</li>
</ul>
</div>
</div>
'''
soup = BeautifulSoup(html,'lxml')
print(soup.find(name='ul'))
print(type(soup.find(name='ul')))
print(soup.find(class_='list'))
# find_parents()返回祖先节点
# find_parent()返回直接父节点
# find_next_siblings()返回后面所有的兄弟节点
# find_next_sibling()返回后面的第一个的兄弟节点
# find_previous_siblings()返回前面所有的兄弟节点
# find_previous_siblings()返回前面第一个兄弟节点
# find_all_next()返回节点后所有符合条件的节点
# find_next()返回节点后所有符合条件的第一个节点
# find_all_previous()返回节点后所有符合条件的节点
# find_previous()返回节点后所有符合条件的第一个节点